<template>
  <div class="contain">
    <!-- 搜索 -->
    <el-form :model="query" ref="queryForm" :inline="true" class="top-search" label-position="left">
      <el-form-item label="标题" prop="sender">
        <el-input v-model="query.title" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作菜单 -->
    <div class="mid-action">
      <div class="total">
        共
        <span class="num">{{ total }}</span> 位客户，实际客户
        <span class="num">{{ total }}</span> 位。
      </div>
    </div>
    <!-- 表格列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="ID" align="center" prop="wxUserId" />
      <el-table-column label="昵称" align="center" prop="wxUserName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="发布时间" align="center" prop="createTime" />
      <el-table-column label="邀请人" align="center" prop="wxInviteUserName"></el-table-column>
      <el-table-column label="标签" align="center" prop="weTags" width="200">
        <template slot-scope="scope">
          <el-tag type="info" v-for="(item,index) in scope.row.weTags" :key="index">{{item.name}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="type">
        <template slot-scope="scope">
          <span>{{ scope.row.type == 1 ? '自然注册' : '邀请注册' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="$router.push({ path:'/wxmini/logs', query:{ id: scope.row.wxUserId } }  )">足迹</el-button>
          <el-button size="mini" type="text" @click="showTags(scope.row,'add')">打标签</el-button>
          <el-button size="mini" type="text" v-if="scope.row.weTags && scope.row.weTags.length > 0" @click="showTags(scope.row,'delete')">移除标签</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList()"
    />
    <!-- 编辑表单 -->
    <el-dialog :title="formTtile == 'creat' ? '添加' : '编辑'" :visible.sync="editFormShow">
      <el-form class="edit_form" :model="form" ref="form" label-position="left">
        <el-form-item label="标题" prop="sender">
          <el-input class="input" v-model="form.title" placeholder="请输入" clearable  />
        </el-form-item>
        <el-form-item label="内容" prop="sender">
          <el-input class="input" type="textarea" v-model="form.content" placeholder="请输入" clearable  />
        </el-form-item>
        <el-form-item label=" ">
          <el-button type="primary" @click="submit()">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 标签 -->
    <el-dialog :title="tagTitle == 'add' ? '增加标签' : '移除标签'" :visible.sync="tagShow">
      <el-form ref="tagForm" :model="tagForm">
        <el-form-item>
          <el-checkbox-group v-model="tagValue">
            <el-checkbox v-for="(item,index) in tags" :key="index" :label="item.name"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="tagSubmit()">确定</el-button>
          <el-button @click="tagShow = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, addtags, deletetags } from "@/api/eh-api";
import { getList as getListTag } from "@/api/customer/tag";
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 查询参数
      query: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
      },
      list:[],
      editFormShow:false,
      formTtile:'creat',
      form:{
        title:'',
        content:'',
      },
      //标签
      tagTitle:'add',
      tags:[],
      tagValue:[],
      tagShow: false,
      tagForm:{
        addTag:[],
        userId:'',
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /**
     * 获取列表
     */
    getList() {
      this.loading = true;
      getUserList(this.query)
        .then(({ rows, total }) => {
          this.list = rows;
          this.total = parseInt(total);
          this.loading = false;
          this.ids = [];
        })
        .catch(() => {
          this.loading = false;
        });
    },
    /**
     * 添加
     */
    creatItem:function(title,item) {
      this.formTtile = title
      this.editFormShow = true
      if( title == 'edit' ){
        this.form = item
      }else{
        this.form = {}
      }
    },

    /**
     * 显示标签列表
     */
    showTags:function(row, type){
      // console.log('row',row.weTags)
      // console.log('选中',this.tagValue)
      this.tagTitle = type
      this.tagValue = [] //初始化
      if( row.weTags && row.weTags.length > 0 ){
        this.tagValue = row.weTags.map( ele => {
            return ele.name
        })
      }
      this.tagForm.userId = row.wxUserId
      if( type == 'add' ){
        getListTag().then( res => {
          if( res.code == 200 ){
            let tags = []
            res.rows.map( ele => {
              tags = [...tags,...ele.weTags]
            })
            this.tags = tags
            this.tagShow = true
            // console.log(this.tags)
          }else{
            this.$message.error(res.msg)
          }
        })
      }else{
        this.tags = row.weTags
        this.tagShow = true
      }
    },

    /**
     * tag表单提交
     */
    tagSubmit:function(){
      console.log('选择的tag',this.tagValue)
      console.log('tags',this.tags)
      let value = []
      if( this.tagTitle == 'add' ){
        value = this.tagValue.map( ele => {
            return this.tags.find( item => item.name == ele)
        })
      }else{
        this.tags.map( ele => {
          if(this.tagValue.indexOf(ele.name) == -1){
            value.push(ele)
          }
        })
      }
      this.tagForm.addTag = value
      console.log(this.tagForm)

      if(this.tagTitle == 'add'){
        addtags(this.tagForm).then( res => {
          if( res.code == 200 ){
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.getList();
            this.tagShow = false
          }else{
            this.$message.error(res.msg);
          }
        })
      }else{
        console.log('删除标签')
        deletetags(this.tagForm).then( res => {
          if( res.code == 200 ){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getList();
            this.tagShow = false
          }else{
            this.$message.error(res.msg);
          }
        })
      }
    },

    /**
     * 提交表单
     */
    submit:function(params) {
        console.log( this.form )
        if( this.formTtile == 'creat' ){
          creatMessage(this.form).then( res => {
            if( res.code == 200 ){
              this.$message({
                message: '添加成功',
                type: 'success'
              });
              this.getList();
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          editMessage(this.form).then( res => {
              if( res.code == 200 ){
                this.$message({
                  message: '编辑成功',
                  type: 'success'
                });
                this.getList();
              }else{
                this.$message.error(res.msg);
              }
            })
        }
        this.editFormShow = false
    },
    /**
     * 删除
     */
    deleteItem:function(rows) {
        console.log(rows)
        deleteMessage({
          newsId:rows.newsId
        }).then( res => {
            if( res.code == 200 ){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.getList();
            }else{
              this.$message.error(res.msg);
            }
        })
    },
    /**
     * 统一路由
     */
    route(row) {
      console.log(row)
      this.$router.push({ path: 'creat', query: { id:row.id } })
    },
  },
};
</script>

<style lang="scss" scoped>
.mid-action {
  display: flex;
  justify-content: space-between;
  margin: 10px 0;
  align-items: center;
  .total {
    background-color: rgba(65, 133, 244, 0.1);
    border: 1px solid rgba(65, 133, 244, 0.2);
    border-radius: 3px;
    font-size: 14px;
    min-height: 32px;
    line-height: 32px;
    padding: 0 12px;
    color: #606266;
  }
  .num {
    color: #00f;
  }
}
.thumbnail {
  width: 160px;
  height: 80px;
}
.input{
  width: 80%;
}
</style>
